package com.example.demo.controller;

import com.example.demo.config.ResponseModel;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Reply;
import com.example.demo.service.CommentService;
import com.example.demo.service.ReplyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private ReplyService replyService;

    @GetMapping("/getAllComments")
    @ApiOperation(value="查询所有评论信息",tags={"查询评论信息"},httpMethod = "GET")
    public ArrayList<Comment> getAllComments() {
        return new ArrayList<>(commentService.getAllComments());
    }

    @GetMapping("/getCommentsByOrderId")
    @ApiOperation(value="通过订单id查询评论信息",tags={"查询评论信息"},httpMethod = "GET")
    public ArrayList<Comment> getCommentsByOrderId(@RequestParam Long orderId){
        return new ArrayList<>(commentService.getCommentsByOrderId(orderId));
    }

    @GetMapping("/getCommentsByUserId")
    @ApiOperation(value="根据用户id查询评论信息",tags={"查询评论信息"},httpMethod = "GET")
    public ArrayList<Comment> getCommentsByUserId(@RequestParam Long userId) {
        return  new ArrayList<>(commentService.getCommentsByUserId(userId));
    }

    @GetMapping("/getCommentsByUserIdAndOrderId")
    @ApiOperation(value="根据用户id和订单id查询评论信息",tags={"查询评论信息"},httpMethod = "GET")
    public Comment getCommentsByUserIdAndOrderId(@RequestParam Long userId, @RequestParam Long orderId) {
        return commentService.getCommentsByUserIdAndOrderId(userId,orderId);
    }

    @DeleteMapping("/deleteCommentByCommentId")
    @ApiOperation(value="根据评论id删除评论",tags={"删除评论"},httpMethod = "DELETE")
    public boolean deleteCommentByCommentId(@RequestParam Long commentId) {
        commentService.deleteCommentByCommentId(commentId);
        return true;
    }

    @DeleteMapping("/deleteCommentByUserIdAndOrderId")
    @ApiOperation(value="根据用户id和订单id删除评论",tags={"删除评论"},httpMethod = "DELETE")
    public boolean deleteCommentByUserIdAndOrderId(@RequestParam Long userId, @RequestParam Long orderId) {
        commentService.deleteCommentByUserIdAndOrderId(userId,orderId);
        return true;
    }

    @PostMapping("/newComment")
    @ApiOperation(value="新建评论",tags={"新建评论"},httpMethod = "POST")
    public void newComment(@RequestParam Long orderId, @RequestParam String content, @RequestParam Long userId) {
        Comment comment = new Comment(orderId,content,userId,String.valueOf(userId),new Date());
        commentService.newComment(comment);
    }

    @GetMapping("/getAllReply")
    @ApiOperation(value="查询所有回复信息",tags={"查询回复信息"},httpMethod = "GET")
    public List<Reply> getAllReply() {
        return replyService.getAllReply();
    }

    @GetMapping("/getReplyByCommentId")
    @ApiOperation(value="根据评论id查询回复",tags={"查询回复信息"},httpMethod = "GET")
    public Reply getReplyByCommentId(@RequestParam Long commentId) {
        return replyService.getReplyByCommentId(commentId);
    }

    @GetMapping("/getReplyByAdminId")
    @ApiOperation(value="根据管理员id查询回复（暂时无用）",tags={"查询回复信息"},httpMethod = "GET")
    public ArrayList<Reply> getReplyByAdminId(@RequestParam Long adminId) {
        return  new ArrayList<>(replyService.getReplyByAdminId(adminId));
    }

    @DeleteMapping("/deleteReplyByCommentId")
    @ApiOperation(value="根据评论id删除回复",tags={"删除回复"},httpMethod = "DELETE")
    public void deleteReplyByCommentId(@RequestParam Long commentId) {
        replyService.deleteReplyByCommentId(commentId);
    }

    @DeleteMapping("/deleteReplyByReplyId")
    @ApiOperation(value="根据回复id删除回复",tags={"删除回复"},httpMethod = "DELETE")
    public void deleteReplyByReplyId(@RequestParam Long replyId) {
        replyService.deleteReplyByReplyId(replyId);
    }


    @PostMapping("/newReply")
    @ApiOperation(value="新建回复",tags={"新建回复"},httpMethod = "POST")
    public void newReply(@RequestParam Long commentId, @RequestParam String content, @RequestParam Long adminId) {
        Reply reply = new Reply(commentId,content,adminId,String.valueOf(adminId),new Date());
        replyService.newReply(reply);
    }
}
